<?php
require_once dirname(__FILE__).'/Example.php';
require_once dirname(__FILE__).'/Comment.php';

if (!defined('T_DOC_COMMENT'))
	define('T_DOC_COMMENT', T_COMMENT);

/**
 * doctest를 위한 PHP 코드
 *
 * 생성자에는 PHP 코드를 넘깁니다.
 * php> $source = new DocTestSource("<?php\n/*\nsome comment *" . "/ ?>");
 *
 * getDocComments 메소드는 해당 코드에 포함된 주석을 추출하여 DocTestComment 객체로 돌려줍니다.
 * php> $comments = $source->getDocComments();
 * php> $comments[0]->text
 * "\nsome comment "
 * php> $comments[0]->lineNumber
 * 2
 */
class DocTestSource {
	function DocTestSource($source) {
		$this->source = $source;
	}

	function getDocComments() {
		$comments = array();
		$lineno = 1;
		foreach (token_get_all($this->source) as $token) if (!is_string($token)) {
			if ($token[0] == T_DOC_COMMENT && substr($token[1], 0, 2) == '/*') {
				$comments[] = new DocTestComment(substr($token[1], 2, -2), $lineno);
			}
			$lineno += substr_count($token[1], "\n");
		}
		return $comments;
	}
}
